package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.configure.Result;
import com.hospital.entity.FeeRecord;
import com.hospital.mapper.CaseInfoMapper;
import com.hospital.mapper.FeeRecordMapper;
import com.hospital.service.FeeRecordService;
import com.hospital.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WuLiting
 * @since 2021-03-24
 */
@Service
public class FeeRecordServiceImpl extends ServiceImpl<FeeRecordMapper, FeeRecord> implements FeeRecordService {

    @Resource
    FeeRecordMapper feeRecordMapper;

    @Resource
    CaseInfoMapper caseInfoMapper;

    @Override
    public Result<FeeRecord> updateFeeRecord(FeeRecord feeRecord) {
        Result<FeeRecord> result = new Result<>();
        try {
            if (StringUtils.isEmpty(feeRecord.getRecordId())) {
                //如果ID为空，代表添加
                feeRecord.setRecordId(IDGenerator.getUUID("RECORD"));
                feeRecordMapper.insert(feeRecord);
            } else {
                //如果ID不为空，代表修改
                feeRecordMapper.updateById(feeRecord);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<FeeRecord> delFeeRecord(FeeRecord feeRecord) {
        Result<FeeRecord> result = new Result<>();
        try {
            if (StringUtils.isEmpty(feeRecord.getRecordId())) {
                result.setMessage("feeRecordId can't be empty");
                return result;
            }
            feeRecordMapper.deleteById(feeRecord.getRecordId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<FeeRecord> batchDelFeeRecord(FeeRecord feeRecord) {
        Result<FeeRecord> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(feeRecord.getIds())) {
                String[] ids = feeRecord.getIds().split(",");
                for (String id : ids) {
                    feeRecordMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<FeeRecord> queryFeeRecord(FeeRecord feeRecord) {
        Result<FeeRecord> result = new Result<>();
        try {
            QueryWrapper<FeeRecord> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用

            List<FeeRecord> feeRecords = feeRecordMapper.selectList(wrapper);
            result.setReturnList(feeRecords);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<FeeRecord> queryFeeRecordByPage(FeeRecord feeRecord) {
        Result<FeeRecord> result = new Result<>();

        try {
            if (feeRecord.getCurrent() == null || feeRecord.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<FeeRecord> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if (!StringUtils.isEmpty(feeRecord.getUserName())) {
                wrapper.lambda().like(FeeRecord::getUserName, feeRecord.getUserName());
            }

            IPage<FeeRecord> feeRecords = feeRecordMapper.selectPage(new Page<>(feeRecord.getCurrent(), feeRecord.getSize()), wrapper);

            result.setReturnPage(feeRecords);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<FeeRecord> queryFeeRecordById(FeeRecord feeRecord) {
        Result<FeeRecord> result = new Result<>();
        try {
            FeeRecord returnFeeRecord = feeRecordMapper.selectById(feeRecord.getRecordId());
            result.setReturnObj(returnFeeRecord);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
